﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace Tools
{
    public partial class Main : Form
    {
        XmlNode node;
        public Main()
        {
            InitializeComponent();
            XmlDocument navXml = new XmlDocument();
            navXml.Load(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"config/navigation.xml"));
            node = navXml.DocumentElement;
        }

        private void Main_Load(object sender, EventArgs e)
        {
            this.Controls.Add(new NavPanel(node));
        }

        private void btMain_Click(object sender, EventArgs e)
        {
            foreach (Control control in this.Controls)
            {
                if (control is NavPanel)
                    this.Controls.Remove(control);
            }

            this.Controls.Add(new NavPanel(node));
        }

        private Control FindControl<T>(Control.ControlCollection controls)
        {
            Control control = null;

            foreach (Control item in controls)
            {
                if (item is T)
                    return item;
                else
                    control = FindControl<T>(item.Controls);
            }

            return control;
        }

        private void btBack_Click(object sender, EventArgs e)
        {
            MUserControl baseUC = FindControl<MUserControl>(this.Controls) as MUserControl;
            if (baseUC != null)
            {
                Control parent = baseUC.Parent;
                foreach (Control item in parent.Controls)
                {
                    if (item == baseUC)
                        parent.Controls.Remove(baseUC);
                    else
                        item.Show();
                }
                return;
            }

            NavButton navButton = FindControl<NavButton>(this.Controls) as NavButton;
            if (navButton == null) return;

            XmlNode node = navButton.NavXmlNode.ParentNode.ParentNode;
            if (node == null || node.NodeType == XmlNodeType.Document) return;

            foreach (Control control in this.Controls)
            {
                if (control is NavPanel)
                    this.Controls.Remove(control);
            }
            this.Controls.Add(new NavPanel(node));
        }

        private void button1_MouseHover(object sender, EventArgs e)
        {

        }
    }
}
